import numpy as np
import pandas as pd
from openpyxl import load_workbook

import print_ans
import 填表ABC2
import 填表DEF2

# Excel 文件路径
excel_filename = "data/result2.xlsx"


# 定义数据填充逻辑
def fill_data_to_excel(seasonal_data, filename):
    # 加载已有的 Excel 文件
    book = load_workbook(filename)

    # 遍历每一年和每个季节的数据
    for year in range(2024, 2031):
        sheet_name = str(year)
        if sheet_name not in book.sheetnames:
            print(f"Sheet {sheet_name} not found in the workbook.")
            continue

        sheet = book[sheet_name]  # 获取对应年份的工作表

        for season in ["第一季", "第二季"]:
            key = f"{year}_{season}"
            data_array = seasonal_data[key]

            # 确定数据写入的位置
            if season == "第一季":
                start_row, start_col = 2, 3  # 对应Excel的第二行第三列
            elif season == "第二季":
                start_row, start_col = 30, 3  # 对应Excel的第56行第三列

            # 将数据逐个填充到指定的单元格中
            for i, row_data in enumerate(data_array):
                for j, value in enumerate(row_data):
                    # 将值写入Excel的对应单元格
                    if season == "第二季" and i <= 25:
                        continue
                    sheet.cell(row=start_row + i, column=start_col + j, value=value)

    # 保存 Excel 文件
    book.save(filename)


# 调用其他函数并更新数据
填表ABC2.tianabc()
填表DEF2.tiandef()

# 获取填充数据
seasonal_data = print_ans.seasonal_data

# 填充数据到 Excel
fill_data_to_excel(seasonal_data, excel_filename)
